//
//  MineWealthViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/15.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "MineWealthViewController.h"
#import "RootWebViewController.h"
#import "MyProfitViewController.h"
#import "RechargeViewController.h"
@interface MineWealthViewController ()
@property(nonatomic, strong) UIButton *backButton;//返回按钮
@property(nonatomic, strong) UILabel *labtitle;//标题
@property(nonatomic, strong) UIButton *detailedButton;//明细
@property(nonatomic, strong) UILabel *totalCoinLab;//总聊币
@property(nonatomic, strong) UILabel *totalCoinNumber;//总聊币数量
@property(nonatomic, strong) UIImageView *headerImageView;//头部背景
@property(nonatomic, strong) QMUIFillButton *myProfit;//我的收益按钮
@property(nonatomic, strong) QMUIFillButton *buyCoin;//购买聊币

@end

@implementation MineWealthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)setupUI
{
    self.isHidenNaviBar = YES;
    _backButton = [[UIButton alloc] init];
//    _backButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_backButton addTarget:self action:@selector(handleBackEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_backButton setImage:IMAGE_NAMED(@"icon_back") forState:UIControlStateNormal];
    
    _detailedButton = [[UIButton alloc] init];
    [_detailedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_detailedButton addTarget:self action:@selector(handleDetailEvent) forControlEvents:UIControlEventTouchUpInside];

    [_detailedButton setTitle:ASLocalizedString(@"明细")forState:UIControlStateNormal];
    _detailedButton.titleLabel.font = DEFAULT_FONT(13);
//    _detailedButton.backgroundColor = [UIColor blackColor];
    _labtitle = [[UILabel alloc] init];
    _labtitle.text = ASLocalizedString(@"我的财富");
    _labtitle.textColor = [UIColor whiteColor];
    
    _totalCoinLab = [[UILabel alloc] init];

    _totalCoinLab.text  = [NSString stringWithFormat:ASLocalizedString(@"总%@"),KCOIN_NAME];
    _totalCoinLab.font = DEFAULT_FONT(13);
    _totalCoinLab.textColor = KWhiteColor;
    
    _totalCoinNumber = [[UILabel alloc] init];
    _totalCoinNumber.text = @"...";
    _totalCoinNumber.textColor = [UIColor whiteColor];
    _totalCoinNumber.font =  [UIFont fontWithName:@"Helvetica-Bold" size:28];
    
    _headerImageView = [[UIImageView alloc] init];
    _headerImageView.backgroundColor = MainColor;

    [self.view addSubview:_headerImageView];

    [_headerImageView addSubview:_labtitle];
    [_headerImageView addSubview:_totalCoinLab];
    [_headerImageView addSubview:_totalCoinNumber];
    
    _myProfit = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_myProfit setTitle:ASLocalizedString(@"我的收益")forState:UIControlStateNormal];
    [_myProfit addTarget:self action:@selector(handleProfitEvent) forControlEvents:UIControlEventTouchUpInside];
    if (IsCurrentUserMale) [_myProfit setHidden:YES];
    
    
    _buyCoin = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_buyCoin setTitle:NSStringFormat(ASLocalizedString(@"购买%@"),KCOIN_NAME) forState:UIControlStateNormal];
    [_buyCoin addTarget:self action:@selector(handleBuyCoinEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_detailedButton];
    [self.view addSubview:_backButton];
    [self.view addSubview:_myProfit];
    [self.view addSubview:_buyCoin];


    [self loadData];
    [self setLayout];
}

- (void)handleBuyCoinEvent {
    RechargeViewController *recharge = [RechargeViewController new];
    recharge.title = ASLocalizedString(@"充值");
    [self.navigationController pushViewController:recharge animated:NO];
}



//跳往我的收益界面
- (void)handleProfitEvent {
    MyProfitViewController *myprofit = [[MyProfitViewController alloc] init];
    [self.navigationController pushViewController:myprofit animated:YES];
}

- (void)handleDetailEvent {
    
    NSString *url = NSStringFormat(@"%@?&uid=%@&token=%@",KGlobalVariable.appmodel.app_h5.my_detail,curUser.id,curUser.token);
    RootWebViewController *web = [[RootWebViewController alloc] initWithUrl:url];

    [self.navigationController pushViewController:web animated:NO];
}

-(void)loadData
{
//    http://192.168.0.109/mapi/public/index.php/api/page_data_api/get_wealth_page_info?uid=100114&token=0a846a208621426c5377e383b0ddabca
    
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"get_wealth_page_info"];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //[param setObject:@"" forKey:@""];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        
        _totalCoinNumber.text = [NSString stringWithFormat:@"%@",[responseObject valueForKey:@"coin"]];
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
    
}

-(void)handleBackEvent:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setLayout
{
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(0.35);
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
    }];
    
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.top.equalTo(self.view).offset(kStatusBarHeight);
        make.width.equalTo(@40);
        make.height.equalTo(@40);
    }];
    
    [_detailedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.equalTo(@40);
        make.centerY.equalTo(_backButton);
        make.height.equalTo(@40);
    }];
    
    [_labtitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
//        make.top.equalTo(self.view).offset(8);
        make.centerY.equalTo(_backButton);
    }];
    
    [_totalCoinLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_labtitle.mas_bottom).offset(25);
        make.height.equalTo(@20);
    }];
    
    [_totalCoinNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_totalCoinLab.mas_bottom).offset(25);
    }];
    
    [_myProfit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-160);
        make.width.equalTo(self.view).offset(-70);
        make.height.equalTo(@40);
    }];
    
    [_buyCoin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(_myProfit.mas_top).offset(-20);
        make.width.equalTo(self.view).offset(-70);
        make.height.equalTo(@40);
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//隐藏掉状态栏
//- (BOOL)prefersStatusBarHidden {
//    return YES;
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
